<?php

namespace App\Http\Controllers\Vhome;

use Illuminate\Http\Request;
use DB;
use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function login()
	{
		//登陆界面
		return view('vhome/login');
	}
	//检测登录的方法
	public function checkLogin(Request $request)
	{
		//echo 'qqqqqqqqqqqq';
		//接受表单信息
		$uname = $request->input('uname');
		$upwd = $request->input('upwd');
		$upwd=md5($upwd);
		//echo $uname;
		//echo $upwd;
		
		$res=DB::table('user')
						->select('uid','uname','uemail')
						->where('uname','=',$uname)
						->where('upwd','=',$upwd)
			 			->first();
		//dd($res);
		//验证用户是否存在和密码正确
		if($res){
			//查询到用户   
			//写入session信息 
			Session::put('uauth',true);//登录标志
			Session::put('uid',$res['uid']);//用户id
			Session::put('uname',$res['uname']);//用户名称
			Session::put('uemail',$res['uemail']);//用户邮箱
			return redirect('/vhome');
		}else{
			//查不到用户  滚回去
			return redirect('/vhome/login');
		}
	}
	//退出登录方法
	public function logout(Request $request)
	{
		$res=session::flush();
		
		if($res){
			return redirect('/vhome');
		}else{
			return  redirect('/vhome');
		}
	}
}
